package com.tyning.newcode.jz30;

import java.util.Stack;

public class MinStack {
    Stack<Integer> stack1;
    Stack<Integer> stackMin;
    /** initialize your data structure here. */
    public MinStack() {
        stack1 = new Stack<>();
        stackMin = new Stack<>();
    }

    public void push(int x) {
        stack1.add(x);
        // 判断是否引起最小堆的变化
        // 如果添加的元素比栈的首要元素大
        if (stackMin.empty() || x <= stackMin.peek()) {
            stackMin.add(x);
        }
    }

    public void pop() {
        if (!stack1.empty() && !stack1.empty() && stackMin.peek().equals(stack1.pop())) {
            stackMin.pop();
        }
    }

    public int top() {
        return stack1.peek();
    }

    public int min() {
        return stackMin.peek();
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(x);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.min();
 */
